using LDNS.Utilities;
using LDNS.SDK.Commands.Models;

namespace LDNS.Commands
{
    /// <summary>
    /// 删除记录命令处理器
    /// 处理REMOVE命令，删除DNS解析记录
    /// 作者：huobing
    /// </summary>
    public class RemoveCommandHandler : CommandHandlerBase<RemoveCommand, DefaultCommandResult>
    {

        /// <summary>
        /// 命令名称
        /// </summary>
        public override string CommandName => RemoveCommand.NAME;
        
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志记录器</param>
        /// <param name="dnsService">DNS服务实例</param>
        public RemoveCommandHandler()
        {
        }
        
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="args">命令参数，格式：域名</param>
        /// <returns>执行结果</returns>
        public override BaseCommandResult Execute(string[] args)
        {
            try
            {
                if (args.Length < 1)
                {
                    return CreateErrorResult("参数不足，格式: REMOVE 域名");
                }
                
                string domain = args[0];
                
                if (DnsRecordManager.Instance.RemoveDnsRecord(domain))
                {
                    // 保存配置到文件，实现持久化
                    DnsRecordManager.Instance.SaveConfig();
                    return CreateSuccessResult($"OK: 成功删除记录 {domain}");
                }
                else
                {
                    return CreateErrorResult($"ERROR: 删除记录失败，域名 {domain} 不存在");
                }
            }
            catch (System.Exception ex)
            {
                return CreateErrorResult($"ERROR: 删除记录失败: {ex.Message}");
            }
        }
    }
}